package com.hst.web.base.proxy;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.dao.IORDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.web.base.entity.TAccountIdentity;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.entity.TReivewHistory;
import com.hst.web.base.ui.ContractorEmployeeReviewConfirm;
import com.hst.web.common.utils.PapersNumberUtils;

public class ContractorEmployeeReviewConfirmProxy extends DefaultDataProxy {

	@Autowired
	IORDao dao;

	@Override
	public Object save(MetaData data) throws Exception {
		List<ORQuery> querys = new ArrayList<ORQuery>();
		ContractorEmployeeReviewConfirm e = (ContractorEmployeeReviewConfirm) data;
		
		String employeeCode = e.getEmployeeCode();
		
		PapersNumberUtils.validate(e.getPapersNumber(), e.getPapersType());
		
		TCompanyEmployee companyEmployee= dao.get(TCompanyEmployee.class, employeeCode);
		BeanUtils.copyProperties(companyEmployee, e);
		dao.save(companyEmployee);
		
		querys.add(new ORQuery(Op.eq, "employeeCode", employeeCode));
		
		List<TAccountIdentity> accountIdentityList= dao.list(TAccountIdentity.class, querys);
		if (accountIdentityList.size() > 0 ) {
			TAccountIdentity accountIdentity= accountIdentityList.get(0);
			BeanUtils.copyProperties(accountIdentity, e);
			dao.save(accountIdentity);
		}
		
		TReivewHistory reivewHistory = new TReivewHistory();
		BeanUtils.copyProperties(reivewHistory, e);
		reivewHistory.setReviewType("0");	//0-人员复核
		dao.save(reivewHistory);
		
		return e;
	}
}
